//
//  CIBNHomeCategoryController.m
//  CIBN
//
//  Created by ZNGOO on 16/7/21.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNHomeCategoryController.h"
#import "CIBNBlockListModel.h"
#import "CIBNHomeListModel.h"
#import "CIBNCategoryMenuView.h"
#import "CIBNHomePageScrollCell.h"
#import "CIBHomeListCell.h"
#import "CIBNHomeCell.h"
#import "CIBNPlayController.h"



static NSString * homeCell = @"CIBNHomeCell";
static NSString * listCell = @"CIBHomeListCell";
static NSString * scrollCell = @"CIBNHomePageScrollCell";

@interface CIBNHomeCategoryController () <CIBNCategoryMenuViewDelegte, UICollectionViewDataSource, UICollectionViewDelegate>


@property (nonatomic,strong) UICollectionView * collectionView;

@property (nonatomic,strong) CIBNCategoryMenuView * menuView;
/**  分类数据 */
@property (nonatomic,strong) NSMutableArray * blockArray;

/**  主页数据 */
@property (nonatomic,strong) NSMutableArray * homeArray;
/**  list数据 */
@property (nonatomic,strong) NSMutableArray * listArray;

@property (nonatomic,assign)BOOL isRecommend;

/** 组头 */
@property (nonatomic,strong) UILabel * headlabel;

@end

@implementation CIBNHomeCategoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation];
    
    
    [self loadData];
    
    [self createMenuView];
    
    [self createCollectionView];
}

- (void)createCollectionView{
    
    self.isRecommend = YES;
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), ScreenWidth, ScreenHeight - CGRectGetMaxY(self.menuView.frame)) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerClass:[CIBHomeListCell class] forCellWithReuseIdentifier:listCell];
    [self.collectionView registerClass:[CIBNHomeCell class] forCellWithReuseIdentifier:homeCell];
    [self.collectionView registerClass:[CIBNHomePageScrollCell class] forCellWithReuseIdentifier:scrollCell];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:self.collectionView];
    
    self.headlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth, 30)];
    
}
#pragma mark - UICollectionViewFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_isRecommend == YES) {
        switch (indexPath.section) {
            case 0:
            {
                return CGSizeMake(ScreenWidth, ScreenWidth*9/16);
            }
                break;
                
            default:
            {
                if (self.isH < 3) {
                    return CGSizeMake((ScreenWidth - 40) /3, (((ScreenWidth - 40) /3) * 16/9) + 20);
                }
                else{
                    return CGSizeMake((ScreenWidth - 30) /2, (((ScreenWidth - 30) /2) * 9 /16) + 40);
                }
            }
                break;
        }
    }
    else{
        return CGSizeMake((ScreenWidth - 40) /3, (((ScreenWidth - 40) /3) * 16/9) + 20);
    }

}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
   
    if (_isRecommend == YES) {
        switch (section) {
            case 0:
            {
                return UIEdgeInsetsMake(10, 0, 10, 0);
            }
                break;
                
            default:
            {
                return UIEdgeInsetsMake(0, 5, 10, 5);
            }
                break;
        }
    }
    else{
        return UIEdgeInsetsMake(10, 5, 0, 5);
    }

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (_isRecommend == YES) {
        switch (section) {
            case 0:
            {
                return CGSizeMake(0, 0);
            }
                break;
                
            default:
            {
                return CGSizeMake(ScreenWidth, 40);
            }
                break;
        }
    }
    else{
        return CGSizeMake(0, 0);
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if (_isRecommend == YES) {
        return self.blockArray.count - 1;
    }
    else{
        return 1;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_isRecommend == YES) {
        switch (section) {
            case 0:
            {
                return 1;
            }
                break;
                
            default:
            {
                return [self loadSectionNum:section + 1];
            }
                break;
        }

    }
    else{
        return self.listArray.count;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (self.isRecommend == YES) {
        UICollectionReusableView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        CIBNBlockListModel * model = self.blockArray[indexPath.section+1];
        self.headlabel.text = [NSString stringWithFormat:@"  %@",model.Name];
        [view addSubview:self.headlabel];
        view.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.headlabel.text = model.Name;
        
        self.headlabel.backgroundColor = [UIColor whiteColor];
        return view;
    }
    else{
        UICollectionReusableView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        return view;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   
    if (self.isRecommend == YES) {
        switch (indexPath.section) {
            case 0:
            {
                CIBNHomePageScrollCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:scrollCell forIndexPath:indexPath];
                
                cell.pushPlayVCBlock = ^(CIBNHomeListModel * model){
                    CIBNPlayController * playVC = [[CIBNPlayController alloc] init];
                    playVC.model = model;
                    [self.navigationController pushViewController:playVC animated:YES];
                };
                
                [cell setScrollViewImageArr:[self loadScrollViewArr:1]];
                return cell;
            }
                break;
                
            default:
            {
                if (self.isH < 3) {
                    CIBHomeListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:listCell forIndexPath:indexPath];
                    CIBNHomeListModel * model =[self loadScrollViewArr:indexPath.section+1][indexPath.row];
                    
                    [cell setHomeCell:model recomend:0];
                    return cell;
                }
                else{
                    CIBNHomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeCell forIndexPath:indexPath];
                    CIBNHomeListModel * model =[self loadScrollViewArr:indexPath.section+1][indexPath.row];
                    [cell setHomeCell:model];
                    return cell;
                }
             
            }
                break;
        }
    }
    else{
        CIBHomeListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:listCell forIndexPath:indexPath];
        CIBNHomeListModel * model = self.listArray[indexPath.row];
        [cell setHomeCell:model recomend:1];
        return cell;
    }
    

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CIBNHomeListModel * model = [[CIBNHomeListModel alloc] init];
    
    if (_isRecommend == YES) {
        model = [self loadScrollViewArr:indexPath.section+1][indexPath.row];
    }
    else{
        model = self.listArray[indexPath.row];
    }
    CIBNPlayController * playVC = [[CIBNPlayController alloc] init];
    playVC.model = model;
    [self.navigationController pushViewController:playVC animated:YES];
}


#pragma mark - 创建菜单分类
- (void)createMenuView{
    
    self.menuView = [[CIBNCategoryMenuView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 30)];
    self.menuView.delegate = self;
    [self.view addSubview:self.menuView];
    
}

- (void)didSelectItemIndex:(NSUInteger)index{
    NSLog(@"%ld",index);
    
    if (index == 0) {
        self.isRecommend = YES;
//        [self loadData];
        [self.collectionView reloadData];
    }
    else{
        self.isRecommend = NO;
        CIBNHomeListModel * model = [self loadScrollViewArr:0][index - 1];
        NSLog(@"%@",model.Name);
        [self loadListData:model.contentid];
        [self.collectionView reloadData];
    }
}


- (void)loadData{
   
    NSLog(@"%@",self.model.ID);
    [RJNetwoking POST:Domain_URL body:@{
                                        @"Cmdid":@"4672",
                                        @"Seqid":@"22",
                                        @"ProtoVer":@"1",
                                        @"UID":@"3000000001",
                                        @"HID":@"56D7A9EA3A70EC3618391BA1A3CB59EE",
                                        @"TID":@"52490",
                                        @"CliVer":@"72058693549555712",
                                        @"Oemid":@"10001",
                                        @"Epgid":@"9",
                                        @"Sessid":@"14850116173381489434",
                                        @"TermType":@"30",
                                        @"SubjectID":self.model.ID
                                        }
                                         success:^(id  _Null_unspecified responseObject) {
                                            
                                             self.blockArray = [CIBNBlockListModel mj_objectArrayWithKeyValuesArray:responseObject[@"BlockList"]];
                                             self.homeArray = [CIBNHomeListModel mj_objectArrayWithKeyValuesArray:responseObject[@"MenuList"]];
                                             
                                            
                                             for (CIBNBlockListModel * model in self.blockArray) {
                                                  NSLog(@"%@",model.Name);
                                             }
                                             
//                                             NSMutableArray * array = [self loadScrollViewArr:0];
                                             [self.menuView setScrollViewItemsArray:[self loadScrollViewArr:0]];
                                            
                                             
                                             [self.collectionView reloadData];
                                             
                                        } failure:^(NSError * _Nonnull error) {
                                            
                                            [MBProgressHUD bwm_showTitle:@"出错啦" toView:self.view hideAfter:2.0 msgType:BWMMBProgressHUDMsgTypeError];
                                        }];
}

- (void)loadListData:(NSString *)subjectID{

    [RJNetwoking POST:Domain_URL body:@{
                                        @"Cmdid":@"4618",
                                        @"Seqid":@"113",
                                        @"ProtoVer":@"1",
                                        @"UID":@"3000000001",
                                        @"HID":@"049364046517234524AF2C911F6D5228",
                                        @"TID":@"42756",
                                        @"CliVer":@"72058693549555712",
                                        @"Oemid":@"10001",
                                        @"Epgid":@"9",
                                        @"Sessid":@"10411839877922554998",
                                        @"TermType":@"30",
                                        @"SubjectID":subjectID,
                                        @"StartIndex":@"0",
                                        @"GetNum":@"15"
                                        }
              success:^(id  _Null_unspecified responseObject) {
                  
                  self.listArray = [CIBNHomeListModel mj_objectArrayWithKeyValuesArray:responseObject[@"GetSubjectList"]];
                  
                  
                  [self.collectionView reloadData];
                  
              } failure:^(NSError * _Nonnull error) {
                  
                 [MBProgressHUD bwm_showTitle:@"出错啦" toView:self.view hideAfter:2.0 msgType:BWMMBProgressHUDMsgTypeError];
              }];
}


#pragma mark - 计算每组的元素
- (NSMutableArray *)loadScrollViewArr:(NSUInteger)section{
    
    NSMutableArray * array = [[NSMutableArray alloc] init];
    for (int index = 0; index < self.homeArray.count; index++) {
        CIBNBlockListModel * model = self.blockArray[section];
        CIBNHomeListModel * homeModel = self.homeArray[index];
        
        if ([homeModel.blockid isEqualToString:model.BlockID]) {
            [array addObject:homeModel];
        }
    }
    
    return array;
}

#pragma mark - 计算每组个数
- (NSUInteger)loadSectionNum:(NSUInteger)section{
    
    NSUInteger num = 0;
    for (int index = 0; index < self.homeArray.count; index++) {
        CIBNBlockListModel * model = self.blockArray[section];
        CIBNHomeListModel * homeModel = self.homeArray[index];
        
        if ([homeModel.blockid isEqualToString:model.BlockID]) {
            NSLog(@"%@--%@",homeModel.Name,model.BlockID);
            num++;
        }
    }
    
    if (num > 6) {
        return 6;
    }else{
         NSLog(@"---->%ld",num);
        return num;
    }
    
   
}

- (void)setNavigation{
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.leftBtn.frame = CGRectMake(5, 22, 40, 40);
    [self.leftBtn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    
    [self.leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.centerTitle.text = self.model.Name;
    
    self.rightBtn2.hidden = YES;
    [self.rightBtn1 setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
}

- (void)backAction{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
@end
